package com.granada.behaviour.base;

import com.granada.entity.SysRolePermission;
import com.granada.utils.MapperBeanUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public abstract class RolePermissionBehavior implements com.granada.behaviour.interfaces.RolePermissionBehavior {
    public List<Long> getPermissionIdsByRoles(List<Long> roleIds) throws Exception {
        return MapperBeanUtils.getSysRolePermissionMapper().getPermissionIdsByRoles(roleIds);
    }

    public void removeByRoleId(Long roleId) throws Exception {
        MapperBeanUtils.getSysRolePermissionMapper().removeByRoleId(roleId);
    }

    public void removeByPermissionId(Long permissionId) throws Exception {
        MapperBeanUtils.getSysRolePermissionMapper().removeByPermissionId(permissionId);
    }

    public List<Long> getRoleIds(Long permissionId) throws Exception {
        return MapperBeanUtils.getSysRolePermissionMapper().getRoleIds(permissionId);
    }

    public List<Long> getPermissionIdsByRoleId(Long roleId) throws Exception {
        return MapperBeanUtils.getSysRolePermissionMapper().getPermissionIdsByRoleId(roleId);
    }
    public  void addBatch(List<Long> permissionIds,Long roleId) throws Exception{
        Date createTime=new Date();
        List<SysRolePermission> list=new ArrayList<>();
        for (Long permissionId:permissionIds){
            SysRolePermission sysRolePermission = SysRolePermission.builder()
                    .id(null)
                    .createTime(createTime)
                    .permissionId(permissionId)
                    .roleId(roleId)
                    .build();
            list.add(sysRolePermission);
        }
        MapperBeanUtils.getSysRolePermissionMapper().removeByRoleId(roleId);
        MapperBeanUtils.getSysRolePermissionMapper().batchRolePermission(list);
    }


}
